package com.yx.common.core.enums;


import com.yx.common.core.exception.custom.IMyException;

/**
 * description: BaseRequest
 * date: 2019/8/15 10:45
 * author: xijie.wu
 */
public enum ReturnMsgEnum implements IMyException {
    SUCCESS(200,"操作成功")
    ,FAIL(500,"操作失败")
    ,ERROR(999,"系统异常")
    ,PARAMILLEGAL(501,"参数异常")
    ,CODENOTNULL(502,"编码不能为空")
    ,PROJECTCONTAINPERSON(503,"当前项目存在已加入的其他用户，是否继续删除")
    ,REPETITIVE_OPERATION(504,"重复操作")
    ,DATA_NULL_DELETED(505,"数据不存在或已删除")
    ,USER_NAME_NULL(506,"用户名错误")
    ,USER_NAME_PWD_NULL(507,"用户名或密码为空")
    ,USER_NOT_FOUND(508,"用户不存在")
    ,AUTH_USER_DEL_OR_LOCKED(509,"存在用户已删除/已锁定")
    ,USER_LOCKED(510,"用户被停用")
    ,USER_ALREADY_EXISTS(511,"当前用户已存在")
    ,OLD_PWD_ERROR( 512,"旧密码错误")
    ,UPDATE_USERNAME_ERROR(513,"禁止修改账户")
    ,ROLE_NAME_REPEAT(514, "角色名称重复")
    ,ROLE_KEY_REPEAT(515, "角色权限KEY重复")
    ,CODE_PARAMILLEGAL_ERROR(516,"验证码有误")
    /**
     * 服务调用相关异常
     */
    ,SERVER_INVOKE_FAILURE(404,"服务调用失败")
    ,SERVER_NO_ACCESS(401,"没有访问权限")

    /**
     * 数据操作相关异常
     */
    ,DUPLICATION(10000,"数据已存在")
    ,DEL_ERROR_DEPENDENT(10001,"数据有下层依赖，删除失败")
    ,BAD_SQL_GRAMMAR(10002,"SQL语法异常")
    /**
     * 脚本工厂相关异常
     */
    ,LOCK_ERROR(20000,"您当前无权操作，请先解锁")
    ,SAVE_ERROR_BY_COMMIT(20001,"提交中不可以保存，请稍等")
    ,COMMIT_ERROR_BY_SAVE(20002,"保存中不可以提交，请稍等")
    ,WORKFLOW_HAVE_CYCLE(20003,"工作流中存在环，提交失败")
    ,COMMIT_ERROR_BY_START_NODE(20004,"工作流存在多个开始节点，请重新配置之后再次尝试提交")
    ,SQL_EXCUTE_FILED(20005,"执行计划获取失败，请检查连接账号权限")

    //todo Increase code according to business requirements

    /**
     * 项目连接配置
     */
    ,NO_SQL_CONN(-401,"无数据库配置,请先至项目管理配置")
    ,NO_SHELL_CONN(-402,"无shell配置,请先至项目管理配置")
    ,NO_NOTEBOOK_CONN(-403,"无notebook配置,请先至项目管理配置")

    /**
     * 导入工作流相关
     */
    ,IMPORT_ERROR(-200,"工作流导入节点中有部分节点不存在")

    /**
     * main服务相关
     */
    ,MAIN_SQL_EXECUTE(601,"执行脚本失败，脚本内容=[%s]，异常信息=[%s]")
    ,DATA_IS_OCCUPIED(602,"数据被占用不允许删除，请先删除关联数据")
    ,DATA_NAME_EXIST(603,"数据集名称已经存在，请修改后继续操作！")
    ,GROUP_NAME_EXIST(604,"分组名称已经存在，请修改后继续操作！")
    ,VISUAL_NAME_EXIST(605,"可视化名称已经存在，请修改后继续操作！")
    ,TEMPLATE_NAME_EXIST(606,"模版名称已经存在，请修改后继续操作！")
    ,DATA_HAS_CHILDREN(607,"删除的分组还有下级，不允许删除！")


    ;

    private int code;
    private String msg;

    ReturnMsgEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }





    public static ReturnMsgEnum getByCode(int code){
        for(ReturnMsgEnum returnMsgEnum : ReturnMsgEnum.values()){
            if(returnMsgEnum.getCode() == code){
                return returnMsgEnum;
            }
        }
        return null;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }

    public static String getName(int code) {
        for (ReturnMsgEnum result : ReturnMsgEnum.values()) {
            if (result.getCode() == code) {
                return result.name();
            }
        }
        return null;
    }

    public static Object getCode(String name) {
        for (ReturnMsgEnum result : ReturnMsgEnum.values()) {
            if (result.getMsg().equals(name)) {
                return result.code;
            }
        }
        return null;
    }
}
